﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using OneDayModel.Interfaces;
using OneDayModel.Model;
using OneDayModel.PresentationLogic;

namespace OneDayModel.Web
{
    public partial class RegistrationForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                SetView(NewUserProfileUI);

            
        }

        private void SetView(NewUserWebRegistrationUI newUserProfileUI)
        {
            foreach (string availableRole in newUserProfileUI.AvailableRoles)
            {
                availableRoles.Items.Add(availableRole);
            }
        }

        NewUserWebRegistrationUI NewUserProfileUI
        {
            get
            {
                if (Session[typeof(INewUserProfileUI).Name] == null)
                {
                    NewUserWebRegistrationUI userWebRegistrationUI = new NewUserWebRegistrationUI();

                    IRepository repository = (IRepository) Application[typeof (IRepository).Name];

                    new NewUserProfileUILogic(userWebRegistrationUI, new NewUserProfileModel(repository));
                    Session[typeof (INewUserProfileUI).Name] = userWebRegistrationUI;

                    return userWebRegistrationUI;
                }
                else
                    return (NewUserWebRegistrationUI) Session[typeof (INewUserProfileUI).Name];

            }
        }

        protected void availableRoles_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckBoxList rolesSelector = (CheckBoxList) sender;
            List<string> selectedRoles = new List<string>();
            foreach (ListItem item in rolesSelector.Items)
            {
                if (item.Selected)
                    selectedRoles.Add(item.Text);
            }

            NewUserProfileUI.SelectRoles(selectedRoles.ToArray());

            ShowAttributes();
        }

        private void ShowAttributes()
        {
            registrationPanel.Controls.Clear();
            if (NewUserProfileUI.AttributesForWeb != null && NewUserProfileUI.AttributesForWeb.Count != 0)
            {
                foreach (AttributeRecord attributeRecord in NewUserProfileUI.AttributesForWeb)
                {
                    RegistrationField registrationField = (RegistrationField) LoadControl("RegistrationField.ascx"); //new RegistrationField();
                    registrationField.Caption = attributeRecord.RoleAttribute.Name;
                    registrationField.Value = attributeRecord.Value;
                    registrationField.Required = attributeRecord.RoleAttribute.Required;
                    registrationField.Visible = true;
                    registrationField.ID = attributeRecord.RoleAttribute.Name;
                    registrationField.InitializeAsUserControl(this);

                    registrationPanel.Controls.Add(registrationField);
                }

                registrationPanel.Visible = true;
            }
            else
                registrationPanel.Visible = false;
        }
    }

    internal class NewUserWebRegistrationUI : INewUserProfileUI
    {
        private readonly List<IAttributeRecord> attributesForWeb = new List<IAttributeRecord>();
        private readonly List<string> availableRoles = new List<string>();
        #region INewUserProfileUI Members

        public event EventHandler<RolesSelectionArgs> onRolesSelection;
        public event EventHandler<SaveProfileArgs> onSaveProfile;
        public event EventHandler<SaveProfileArgs> onReturnToQuestioner;

        public void SelectRoles(string[] roles)
        {
            if (onRolesSelection!=null)
                onRolesSelection(this, new RolesSelectionArgs(roles));
        }

        public List<IAttributeRecord> AttributesForWeb
        {
            get { return attributesForWeb; }
        }

        public List<string> AvailableRoles
        {
            get { return availableRoles; }
        }

        public void SetAvailableRoles(string[] roles)
        {
            AvailableRoles.AddRange(roles);
        }

        public void ShowAttributes(IEnumerable<IAttributeRecord> attributes)
        {
            attributesForWeb.Clear();
            attributesForWeb.AddRange(attributes);
        }

        public void ShowNewEMailOption()
        {
            throw new NotImplementedException();
        }

        public void ShowRetrieveCredantials(string email)
        {
            throw new NotImplementedException();
        }

        public void ShowMissingAttributesInfo(string[] missedAttributes)
        {
            throw new NotImplementedException();
        }

        public void ShowBackToQuestioner()
        {
            throw new NotImplementedException();
        }

        #endregion

    }
}
